var wxDraw = require("wxdraw.min.js").wxDraw;
var Shape = require("wxdraw.min.js").Shape;

var queue = [];//发射队列.

function Barrage(wxCanvas, cw, ch) {
  this.wxCanvas = wxCanvas;
  this.cw = cw;
  this.ch = ch;
  
  this.fire = function (color, content, fontSize) {

    var y = RandomNumBoth(20, 120);
    
    var rect = new Shape('text', {
      x: -499,
      y: y,
      fontSize: fontSize,//字体大小
      fillStyle: color,//颜色
      text: content//字
    }, 'fill', true);
    this.wxCanvas.add(rect);
    var offsetX = rect.Shape.offset.x; //偏移量x .我就当做是图形的宽度吧

    //弹幕的冲刺效果(x+=的阀值是我尝试出来的.我暂时没找出规律)
    rect.animate({
      "x": "+=499"
    }, {
        duration: 1500
    }).
    //弹幕缓缓向前(超出屏幕销毁)
    animate({
      "x": "+=" + (cw + offsetX)//为什么要加上要宽度加上偏移量.为了在弹幕在屏幕右侧移除时不会  "闪现" 
    }, {
        duration: 3000
    }).start(1);//只播放一次
  }
}
function RandomNumBoth(Min, Max) {
  var Range = Max - Min;
  var Rand = Math.random();
  var num = Min + Math.round(Rand * Range); //四舍五入
  return num;
}

module.exports = Barrage;